package edu.pku.sei.modeler.neditor.command;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.notation.core.NotationFactory;
import edu.pku.sei.modeler.model.notation.core.ShapeNode;
import edu.pku.sei.modeler.model.notation.shape.Shape;
import edu.pku.sei.modeler.neditor.model.Diagram;

public class DeleteNodeCommand extends Command {
	private MetaModelElement parent = null;
	private MetaModelElement child = null;
	private NotationFactory factory = null;
	
	private String typeString = null;

	public void setChild(MetaModelElement child) {
		this.child = child;
	}

	public void setFactory(NotationFactory factory) {
		this.factory = factory;
	}

	public void setParent(MetaModelElement parent) {
		this.parent = parent;
	}

	@Override
	public boolean canExecute() {
		return true;
	}

	//private Rectangle bound = null;
	
	@Override
	public void execute() {
		if(parent instanceof Diagram){
			Diagram d =(Diagram)parent;
			d.removeNode(child);
		}else if(parent instanceof Shape){
			Shape s = (Shape)parent;
			s.removeSlave((ShapeNode)child);
		}
		//add by fengchao
		typeString = factory.getPackage().getTypeString(child.getElementID());
		//add end
		factory.unRegisterFromPackage(child);
	}

	@Override
	public void redo() {
		execute();
	}

	@Override
	public void undo() {
		//factory.registerToPackage(element, typeString)
		
		//add by fengchao
		factory.registerToPackage(child, typeString);
		if(parent instanceof Diagram){
			Diagram d =(Diagram)parent;
			d.addNode(child);
		}else if(parent instanceof Shape){
			Shape s = (Shape)parent;
			s.addSlaveWithInitBounds((ShapeNode)child, null);
		}
		//add end
	}

}
